package com.insleton.admin.employee.service.impl;

import com.insleton.admin.employee.service.GenericService;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

@Transactional
public class GenericServiceImpl<T, ID> implements GenericService<T, ID> {
    @Inject
    private JpaRepository<T, ID> jpaRepository;

    @Override
    public T save(T t) {
        return jpaRepository.save(t);
    }

    @Override
    public T get(ID id) {
        Optional<T> optional = jpaRepository.findById(id);
        return optional.orElse(null);
    }

    @Override
    public T update(T t) {
        return jpaRepository.save(t);
    }

    @Override
    public int delete(T t) {
        jpaRepository.delete(t);
        return 1;
    }

    @Override
    public int deleteById(ID id) {
        jpaRepository.deleteById(id);
        return 1;
    }

    @Override
    public List<T> queryList(T t) {
        return jpaRepository.findAll(Example.of(t));
    }

    @Override
    public List<T> pagedList(T t) {
        return jpaRepository.findAll(Example.of(t));
    }
}
